package com.activities;

import com.data.DataManager;
import com.main.R;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class History extends ListActivity{
	
	private Cursor dbCursor;
	private DataManager dbManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weights_list);
		dbManager = new DataManager(this);
		dbManager.open();
		fillData();
		registerForContextMenu(getListView());
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, R.id.edit, 0, R.string.edit);
		menu.add(0, R.id.delete, 0,  R.string.delete);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
			case R.id.edit:
				Intent i = new Intent(this, Log.class);
				Cursor cursor = dbCursor;
			    cursor.moveToPosition(info.position);
				i.putExtra(DataManager.KEY_ROWID, info.id);
			    i.putExtra(DataManager.KEY_VALUE, cursor.getString (cursor.getColumnIndexOrThrow(DataManager.KEY_VALUE)));
			    i.putExtra(DataManager.KEY_DATE, cursor.getString (cursor.getColumnIndexOrThrow(DataManager.KEY_DATE)));
			    i.putExtra(DataManager.KEY_TIME, cursor.getString (cursor.getColumnIndexOrThrow(DataManager.KEY_TIME)));
			    startActivityForResult(i, MyActivity.EDIT_LOG);
			return true;
			case R.id.delete:
				dbManager.deleteEntry(info.id);
				fillData();
			return true;
			default:
			return super.onContextItemSelected(item);
		}
	}
	
	/**
     * Does different actions depending on which activity has finished.
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    	// Check that the activity that finished its action, has finished OK. 
    	if (resultCode == RESULT_OK) {
	    	super.onActivityResult(requestCode, resultCode, intent);
	    	Bundle extras = intent.getExtras();
	    	String value = extras.getString(DataManager.KEY_VALUE);
		    String date = extras.getString(DataManager.KEY_DATE);
		    String time = extras.getString(DataManager.KEY_TIME);
		    Long rowId = extras.getLong(DataManager.KEY_ROWID);
	    	switch(requestCode) {
		    	case MyActivity.EDIT_LOG:
		    	    dbManager.updateEntry(rowId, value, date, time);
		    		fillData();
		    	break;
	    	}
	   	}
    }
    
	/**
	 * Brings all data from the database, and sets it to the current display.
	 * */
	private void fillData () {
		dbCursor = dbManager.fetchAllEntries();
		startManagingCursor(dbCursor);
		 // Create an array to specify the fields we want to display in the list (only DATE & VALUE)
        String[] from = new String[]{DataManager.KEY_DATE, DataManager.KEY_VALUE};
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.col1, R.id.col2};
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter weights = new SimpleCursorAdapter(this, R.layout.weights_row, dbCursor, from, to);
        setListAdapter(weights);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_options_menu, menu);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case R.id.settings:
        	MyActivity.onCreateNewActivity(this, MyActivity.SETTINGS);
            return true;
        case R.id.close:
        	MyActivity.onCreateNewActivity(this, MyActivity.CLOSE_APP);
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
	
}
